import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

let reqTimer=null;

export default new Vuex.Store({
  state: {
    msgPopArr:[],
    u_info:{
      address: "",
      amount: "",
      username: "",
      trad_password:null
    }
  },
  mutations: {
    setMsgPop(state,val){
      state.msgPopArr.push(val);
      // setTimeout(()=>{
      //   if(state.msgPopArr.length>5){
      //     console.log("大于5个");
      //     state.msgPopArr.shift();
      //   }
      // },1500);


      // let sliceLen=state.msgPopArr.length;
      // if(sliceLen>5){
      //   if(reqTimer!==null){
      //   	clearInterval(reqTimer);
      //   	reqTimer=null;
      //   }

      //   reqTimer=setTimeout(()=>{
      //     console.log("截取后五个");
      //   	state.msgPopArr=state.msgPopArr.slice(sliceLen-5,sliceLen);
      //   },1500);
      // }

      if(reqTimer!==null){
      	clearInterval(reqTimer);
      	reqTimer=null;
      }

      reqTimer=setTimeout(()=>{
      	state.msgPopArr=[];
      },2000);

    },
    set_u_info(state,val){
      state.u_info=val;
    },
    del_msg_arr(state,payload){
      console.log("msgPopArr要删除的索引：",payload);
      state.msgPopArr.splice(payload,1);
    }
  },
  actions: {
    //Vue中的actions用法
    //https://zhuanlan.zhihu.com/p/374598556
    del_msgPopArr(ctx,payload){
      ctx.commit('del_msg_arr',payload);
    }
  },
  modules: {

  }
})
